<?php
    include($_SERVER['DOCUMENT_ROOT']."/ObstacleWebApp/view/headeradmin.php");
?>

<section>
<hgroup>
<h1>Gebruiker toevoegen</h1>
</hgroup>

    
    
    <script>
function check(input) {
  if (input.value != document.getElementById('email').value) {
    input.setCustomValidity('De 2 email-adressen moeten dezelfde zijn!');
  } else {
    // input is valid -- reset the error message
    input.setCustomValidity('');
  }
}
function passwordStrength(password)
{
	var desc = new Array();
	desc[0] = "Zeer zwak";
	desc[1] = "Zwak";
	desc[2] = "Beter";
	desc[3] = "Medium";
	desc[4] = "Sterk";
	desc[5] = "Sterkst";

	var score   = 0;

	//if password bigger than 6 give 1 point
	if (password.length > 6) score++;

	//if password has both lower and uppercase characters give 1 point	
	if ( ( password.match(/[a-z]/) ) && ( password.match(/[A-Z]/) ) ) score++;

	//if password has at least one number give 1 point
	if (password.match(/\d+/)) score++;

	//if password has at least one special caracther give 1 point
	if ( password.match(/.[!,@,#,$,%,^,&,*,?,_,~,-,(,)]/) )	score++;

	//if password bigger than 12 give another 1 point
	if (password.length > 12) score++;

	document.getElementById("passwordDescription").innerHTML = desc[score];
	document.getElementById("passwordStrength").className = "strength" + score;
}
</script>

<?php if(!isset($_POST['Submit'])) { ?>
<form  name="registerform" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<table>
	<tr>
		<td>Voornaam:</td>
		<td><input required="required" placeholder="Voornaam" name="voornaam" type="text"></td>
	</tr>
        <tr>
		<td>Achternaam:</td>
		<td><input required="required" placeholder="Achternaam" name="achternaam" type="text"></td>
	</tr>
	<tr>
		<td>Email-adres:</td>
		<td><input required="required" placeholder="yourname@example.com" name="email" type="email"></td>
	</tr>
	<tr>
		<td>Herhaal-email adres:</td>
		<td><input required="required" placeholder="yourname@example.com" name="emailrepeat" type="email" oninput="check(this)"></td>
	</tr>
	<tr>
		<td>Wachtwoord:</td>
		<td><input required="required" placeholder="wachtwoord" name="wachtwoord" type="password" onkeyup="passwordStrength(this.value)"></td>
	</tr>
	<tr>
		<td>Herhaal wachtwoord:</td>
		<td><input irequired="required" placeholder="wachtwoord" name="wachtwoordrepeat" type="password"></td>
	</tr>
	<tr>
		<td>Wachtwoord sterkte:	</td>
		<td>
			<div id="passwordDescription">Geen wachtwoord ingegeven</div>
			<div id="passwordStrength" class="strength0"></div>
		</td>
	</tr>
        <tr>
            <td>Functie: </td>
            <td>
                <select name="function">
                <option value="0">Kies een functie</option>
                <?php
                // Get all the possible user functions from the database
                $functions = GetAllUserFunctions();
                while ($functionRow = mysql_fetch_array($functions)){
                    echo "<option value=\"".$functionRow['Id']."\">".$functionRow['Name']."</option>";
                }
                ?>
            </td>
        </tr>
        <tr>
            <td>Geboortedatum: </td>
            <td>
                <?php
                    //get class into the page
                    require_once($_SERVER['DOCUMENT_ROOT'].'/ObstacleWebApp/classes/tc_calendar.php');

                    $myCalendar = new tc_calendar("date5", true, false);
                    $myCalendar->setIcon("../../images/iconCalendar.gif");
                    $myCalendar->setDate(date('d'), date('m'), date('Y'));
                    $myCalendar->setYearInterval(1920, 2050);
                    $myCalendar->dateAllow('1920-01-01', '2050-01-01');
                    $myCalendar->setDateFormat('j F Y');
                    $myCalendar->setAlignment('left', 'bottom');
                    $myCalendar->writeScript();
                ?>
            </td>
        </tr>
        <tr>
            <td>Admin:</td>
            <td>
                <div class="onoffswitch">
                    <input type="checkbox" name="admin" class="onoffswitch-checkbox" id="admin" checked>
                    <label class="onoffswitch-label" for="admin">
                        <div class="onoffswitch-inner">
                            <div class="onoffswitch-active"><div class="onoffswitch-switch">JA</div></div>
                            <div class="onoffswitch-inactive"><div class="onoffswitch-switch">NEE</div></div>
                        </div>
                    </label>
                </div>
            </td>
        </tr>
        <tr>
            <td>Geactiveerd:</td>
            <td>
                <div class="onoffswitch">
                    <input type="checkbox" name="activatie" class="onoffswitch-checkbox" id="activatie" checked>
                    <label class="onoffswitch-label" for="activatie">
                        <div class="onoffswitch-inner">
                            <div class="onoffswitch-active"><div class="onoffswitch-switch">JA</div></div>
                            <div class="onoffswitch-inactive"><div class="onoffswitch-switch">NEE</div></div>
                        </div>
                    </label>
                </div>
            </td>
        </tr>
 	<tr>
            <td><input type="submit" name="Submit" value="Aanmaken"></td>
            <td><input type="button" value="Terug" onclick="javascript:history.back();" /></td>
        </tr>
</table>
</form>
<?php 
} 
else 
{	
        // all the checks on the data that will be inserted
	$error = array();
	if(!check_email($_POST['email'])) {
		$error[] = 'Er is geen geldig e-mailadres ingevuld.';
	}
	
        if($_POST['voornaam'] == "") {
		$error[] = 'Er is geen voornaam ingevuld.';
	}
        
        if (!preg_match('`^[a-zA-Z]+$`',$_POST['voornaam']))
        {
            $error[] = 'Er mogen geen cijfers in de voornaam staan.';
        }
        
        if($_POST['achternaam'] == "") {
		$error[] = 'Er is geen achternaam ingevuld.';
	}

        if (!preg_match('`^[a-zA-Z]+$`',$_POST['achternaam']))
        {
            $error[] = 'Er mogen geen cijfers in de achternaam staan.';
        }
        
        if($_POST['email'] != $_POST['emailrepeat']) {
		$error[] = 'De 2 ingevulde email-adressen komen niet overeen.';
	}
        
        $gebruikersnaam = strtoupper(substr($_POST['voornaam'],0,1)).strtoupper(substr($_POST['achternaam'],0,1)).  strtolower(substr($_POST['achternaam'],1));
        $gebruikersnaam = str_replace (" ", "", $gebruikersnaam);
        
	if($_POST['wachtwoord'] == "" || $_POST['wachtwoord'] != $_POST['wachtwoordrepeat']) {
		$error[] = "Er is geen wachtwoord ingevuld of de 2 wachtwoorden komen niet overeen.";
	}
        
        $check = GetUserCountByEmail($_POST['email']);
	if($check != 0) {
		$error[] = "<strong>".$_POST['email']."</strong> is reeds geregistreerd. Slechts één account per e-mailadres is toegestaan.";
	}
        
        if($_POST['function'] == "") {
		$error[] = 'Er is geen functie ingevuld.';
	}
        
        if (isset($_POST['admin']))
        {
            $admin = "T";
        }
        else
        {
            $admin = "F";
        }
        
        if (isset($_POST['activatie']))
        {
            $activated = "T";
        }
        else
        {
            $activated = "F";
        }
        
        if(!isset($_REQUEST["date5"])) {
		$error[] = 'Er is geen geboortedatum ingevuld.';
	}
        else
        {
            $theDate = date('Y-m-d', strtotime($_REQUEST["date5"]));
        }
        
        list($year,$month,$day) = explode("-",$theDate);
        $year_diff = date("Y") - $year;
        $month_diff = date("m") - $month;
        $day_diff = date("d") - $day;
        if ($month_diff < 0) $year_diff--;
        elseif (($month_diff==0) && ($day_diff < 0)) $year_diff--;
        
        if ($year_diff < 18)
        {
            $error[] = "De gebruiker die je probeert toe te voegen is nog geen 18 jaar dus mag nog niet werken.";
        }
        
        $errors = sizeof($error); // count the amount of errors
        if($errors != 0) { // if there is atleast 1 error, display the error-list
            echo "<p><span style=\"color:red\">Er kon geen account gemaakt worden omwille van de volgende reden(en):</span></p><ul>";
            for($i = 0; $i < $errors; $i++) {
                echo "<span style=\"color:red\"><li>".$error[$i]."</li></span>";
            }
            echo "</ul>";
        }
        else 
        {
            $password = strtoupper($gebruikersnaam).$_POST['wachtwoord'];
            $password = sha1($password);
            
            // inserts the user
            InsertUser($gebruikersnaam, $_POST['voornaam'], $_POST['achternaam'], $password, $_POST['email'], $_POST['function'], $theDate, $admin, $activated);
            
            echo "De gebruiker is toegevoegd.<br>Dit zijn de login-gegevens:<br><ul><li>Gebruikersnaam: ".$gebruikersnaam."</li><li>Wachtwoord: ".$_POST['wachtwoord']."</li></ul>";
            echo "Klik <a href=\"gebruikers.php\">hier</a> om terug te gaan naar het overzicht.";
        }
} 
?>



</section>

<?php
    include($_SERVER['DOCUMENT_ROOT']."/ObstacleWebApp/view/footer.php");
?>